package logic

import (
	"context"
	"doctor-medicine-system/models/mysql"
	"errors"

	"doctor-medicine-system/docrpc/docrpc"
	"doctor-medicine-system/docrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SearchLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSearchLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchLogic {
	return &SearchLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SearchLogic) Search(in *docrpc.SearchRequest) (*docrpc.SearchResponse, error) {
	// todo: add your logic here and delete this line
	var drug mysql.Drug

	if err := drug.FIndDrugID(mysql.DB, in.DurgId); err != nil {
		return nil, errors.New("药品id不存在")
	}
	err := drug.WhereId(mysql.DB, in.DurgId)
	if err != nil {
		return nil, err
	}
	return &docrpc.SearchResponse{
		DurgId:     drug.DocTocId,
		Norms:      drug.Norms,
		Vendor:     drug.Vendor,
		Indication: drug.Indication,
		Dosage:     drug.Dosage,
		Matter:     drug.Matter,
		DocTocId:   drug.DocTocId,
	}, nil
}
